Skip to content

[rust-guard] Deduplicate owner/repo parsing paths in labels backend#5548

Merged
lpcox merged 3 commits into
mainfrom
copilot/rust-guard-deduplicate-loop-bodies
May 12, 2026
Merged

[rust-guard] Deduplicate owner/repo parsing paths in labels backend#5548
lpcox merged 3 commits into
mainfrom
copilot/rust-guard-deduplicate-loop-bodies

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 12, 2026

✨ Enhancement

This change removes two duplication hotspots in guards/github-guard/rust-guard/src/labels/backend.rs without altering behavior: repeated item-scan logic in owner-type extraction and repeated full_name/fullName lookup logic in repo ID extraction. The result is a smaller, more maintainable parsing path with unchanged field precedence and fallback behavior.

What does this improve?

  • Owner-type lookup deduplication
    • Introduces find_org_in_items(items, repo_id) and reuses it for both response shapes:
      • { "items": [...] }
      • plain array [...]
  • Repo ID field lookup deduplication
    • Replaces duplicate if blocks for "full_name" and "fullName" with a single ordered loop.
  • Regression coverage for refactor paths
    • Adds focused tests for:
      • plain-array owner/org extraction path
      • camelCase fullName repo ID extraction path

Why is this valuable?

  • Reduces copy/paste control flow in high-traffic parsing helpers.
  • Centralizes repo-ID match logic used by owner-type extraction.
  • Makes future alias additions and edge-case coverage lower-risk.

Implementation approach:

  • Extracted shared iterator-based helper:
fn find_org_in_items(items: &[Value], repo_id: &str) -> Option<bool> {
    items.iter().find_map(|item| {
        let item_repo_id = repo_id_from_repo_object(item)?;
        if item_repo_id.eq_ignore_ascii_case(repo_id) {
            owner_type_from_repo_object(item)
        } else {
            None
        }
    })
}
  • Updated owner_is_org_from_items to delegate both array shapes to the helper.
  • Updated repo_id_from_repo_object to iterate ["full_name", "fullName"] in priority order, preserving existing behavior.

Copilot AI changed the title [WIP] Refactor backend.rs to deduplicate identical loop bodies [rust-guard] Deduplicate owner/repo parsing paths in labels backend May 12, 2026
Copilot finished work on behalf of lpcox May 12, 2026 14:07
Copilot AI requested a review from lpcox May 12, 2026 14:07
@lpcox lpcox marked this pull request as ready for review May 12, 2026 14:52
Copilot AI review requested due to automatic review settings May 12, 2026 14:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors repo owner/org parsing in the Rust guard labels backend to reduce duplicated logic when extracting owner.type from search results and when deriving repo IDs from repo objects.

Changes:

  • Introduces a shared find_org_in_items(items, repo_id) helper and reuses it for both { "items": [...] } and plain-array response shapes.
  • Deduplicates repo ID extraction by looping over ["full_name", "fullName"] in priority order.
  • Adds regression tests for plain-array owner/org extraction and fullName repo ID extraction.
Show a summary per file
File Description
guards/github-guard/rust-guard/src/labels/backend.rs Deduplicates owner/org lookup and repo-id field parsing; adds tests for the new paths.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment on lines +1386 to +1394
fn find_org_in_items(items: &[Value], repo_id: &str) -> Option<bool> {
items.iter().find_map(|item| {
let item_repo_id = repo_id_from_repo_object(item)?;
if item_repo_id.eq_ignore_ascii_case(repo_id) {
owner_type_from_repo_object(item)
} else {
None
}
})
Comment on lines +1257 to +1262
#[test]
fn test_repo_id_from_repo_object_full_name_camel_case() {
let item = serde_json::json!({
"fullName": "myorg/myrepo",
"owner": { "login": "myorg", "type": "Organization" }
});
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 12, 2026

@copilot merge main and address review feedback

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 12, 2026

@copilot merge main and address review feedback

Merged main and addressed the review feedback in commit 90125676.

  • Preserved first-match semantics in owner/org lookup path (no find_map skip-on-None behavior)
  • Renamed the camelCase repo-id test for clarity
  • Added regression coverage for first matching repo entry with missing owner type returning None

Copilot finished work on behalf of lpcox May 12, 2026 15:32
@lpcox lpcox merged commit ef415e0 into main May 12, 2026
14 checks passed
@lpcox lpcox deleted the copilot/rust-guard-deduplicate-loop-bodies branch May 12, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[rust-guard] Rust Guard: Deduplicate identical loop bodies in backend.rs

3 participants